#ifndef _DEVICE_H
#define _DEVICE_H

#include <type.h>
#include <ds/list.h>
#include <ds/hstr.h>

#define DEV_NAME_LEN 32

#define DEV_TYPE_MASK 0x3

#define DEV_VOLM 0x0 // volume device
#define DEV_CHAR 0x1 // character device
#define DEV_CATE 0x2 // category device (for grouping)

typedef struct device {
    lnode_t children;
    lnode_t siblings;
    u32 id;
    u32 type;
    char name_buf[DEV_NAME_LEN + 1];
    hstr_t name;
    struct device* parent;
    void* fs_node;
    void* impl;
    int (*read)(struct device* dev, void* buf, size_t offset, size_t size);
    int (*write)(struct device* dev, void* buf, size_t offset, size_t size);
} device_t;

device_t* device_add(device_t* parent, void* impl, char* name, u32 name_len, u32 type);

void device_remove(device_t* dev);

device_t* device_getbyhname(device_t* root_dev, hstr_t* name);

device_t* device_getbyid(lnode_t* devlist, u32 id);

device_t* device_getbyoffset(device_t* root_dev, size_t offset);

#endif